$(function(){
	/*输入金额后按钮可点击
	$('input[type="number"]').bind('input propertychange',function(){
		if($(this).val() != ''){
			$(this).parent().find('span').show();
			$(this).css('padding-right','1.2em');
			$('button').removeClass('disabled').removeAttr('disabled')
		}else{
			$(this).parent().find('span').hide();
			$(this).css('padding-right','0');
			$('button').addClass('disabled').attr('disabled','true')
		}
	});*/
	var money = localStorage.rec_money - localStorage.user_balance;
	$('#chargeMoney').val(money.toFixed(2));
	$('#chargeMoney').css('padding-right','1.2em');
	$('.shall').show();
	//获取微信js-sdk配置参数
	$.ajax({
		type:'post',
		url:url() + "/wechat/getWxConfig",
		dataType:'json',
		success:function(result,sta,xhr) {
			var sessionstatus = xhr.getResponseHeader('sessionstatus');
			
			if(sessionstatus == '302'){
				window.location.href = url() + '/wxuser/goCompleteInfo';
			};
			console.log(result);
			localStorage.wxconfig = JSON.stringify(result);
			var wxconfig = JSON.parse(localStorage.wxconfig);
			var wxconfigResult = JSON.parse(wxconfig.result);
			if(wxconfig.code ==0){
				if(wxconfigResult.status ==0) {
					var wxconfigObject = wxconfigResult.object;
					wx.config({
						debug: false,
						appId: wxconfigObject.appId,
						timestamp: wxconfigObject.timestamp,
						nonceStr: wxconfigObject.nonce,
						signature: wxconfigObject.sig,
						jsApiList: [
							'checkJsApi',
							'chooseWXPay'
						]
					});
				}else{
					alert(wxconfigResult.message);
				}
			}else{
				alert(wxconfig.result);
			}
		},
		error:function(){
			alert('error')
		}
	});

	$('.recg .pay').click(function(){
		$.ajax({
			type:'post',
			url:url() + "/userCharge/getOrderForH5",
			data:{
				money:$('#chargeMoney').val()
			},
			dataType:'json',
			success:function(result,sta,xhr) {
				var sessionstatus = xhr.getResponseHeader('redirectStatus');
				if(sessionstatus == '302'){
					window.location.href = url() + '/wxuser/goCompleteInfo';
					return false;
				};

				console.log(result);
				localStorage.order = JSON.stringify(result);
				var orderObject = JSON.parse(localStorage.order);
				if(orderObject.code ==0){
					var orderResult = JSON.parse(orderObject.result);
					if(orderResult.status ==0) {
						var orderInfo = orderResult.object;
						if (typeof WeixinJSBridge == "undefined"){
							if( document.addEventListener ){
								document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
							}else if (document.attachEvent){
								document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
								document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
							}
						}else{
							onBridgeReady(orderInfo);
						}
					}else{
						alert(orderResult.message);
					}
				}else{
					alert(orderObject.result);
				}
			},
			error:function(){
				alert('error')
			}
		})
	})
});

function onBridgeReady(orderInfo){
	//alert(orderInfo.appId+","+orderInfo.timeStamp+","+orderInfo.nonceStr+","+orderInfo.package+","+orderInfo.signType+","+orderInfo.paySign);
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest', {
			"appId" : orderInfo.appId,     //公众号名称，由商户传入
			"timeStamp":orderInfo.timeStamp,         //时间戳，自1970年以来的秒数
			"nonceStr" : orderInfo.nonceStr, //随机串
			"package" : orderInfo.package,
			"signType" : orderInfo.signType,         //微信签名方式：
			"paySign" : orderInfo.paySign //微信签名
		},
		function(res){
			// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
			if(res.err_msg == "get_brand_wcpay_request:ok" ) {
				localStorage.chargeMoney = $('#chargeMoney').val();
				localStorage.orderId = orderInfo.orderId;
				window.location.href = url() + "/htm/recharge-success.html";
			}else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
				localStorage.chargeErrorMessage = "订单已取消";
				window.location.href = url() + "/htm/recharge-error.html";
			}else if(res.err_msg == "get_brand_wcpay_request:fail" ){
				localStorage.chargeErrorMessage = "微信内充值失败";
				window.location.href = url() + "/htm/recharge-error.html";
			}else{
				localStorage.chargeErrorMessage = "微信充值失败";
				window.location.href = url() + "/htm/recharge-error.html";
			}
		}
	);
}